<?php
/**
 * @Author:      zy
 * @DateTime:    2017-09-26
 * @Description: 消息提醒
 */
class ScsRemindController extends BaseController{
 
       /**
    * @Author:    zy
    * @DateTime:  2017-09-26
    * @Description: 当前门店的技师+前台
    */
    public function user_lists(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            $me=MaintenanceEngineers::where('type','shop')->where('type_id',$sc_user->shop_id)->where('status',1)->select('id','name')->get();
            $sc=SCUser::where('shop_id',$sc_user->shop_id)->where('status',1)->select('id','real_name')->get();
            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("me"=>$me,"sc"=>$sc));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "user_lists", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
       /**
    * @Author:    zy
    * @DateTime:  2017-09-26
    * @Description: 添加提醒
    */
    public function add_remind(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'color' => 'required',
                'remind_by' => 'required',
                'remind_at' => 'required',
                'content' => 'required',
                'user_type' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $add_remind=new Remind();
            $add_remind->created_by = $sc_user->id;
            $add_remind->remind_by = Input::get("remind_by");
            $add_remind->remind_at = Input::get("remind_at") .":00";
            $add_remind->content = Input::get("content");
            $add_remind->status = 1;
            $add_remind->shop_id = $sc_user->shop_id;;
            $add_remind->color = Input::get("color");
            $add_remind->user_type = Input::get("user_type");
            $result = $add_remind->save() ? true : false;

            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "user_lists", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

       /**
    * @Author:    zy
    * @DateTime:  2017-09-26
    * @Description: 根据时间和关键字查找提醒
    */
    public function search(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            $start_date = Input::get("start_date");
            $end_date =  Input::get("end_date");
            $text = Input::get("text");
            
            //如果没有参数
            if (empty($start_date) && empty($end_date) && empty($text)) {
                $response = Lang::get("scs_error.remind_error_100001");
                break;
            }
            
            $info = Remind::where("shop_id",$sc_user->shop_id);
            
            if(!empty($start_date))$info = $info->where("remind_at",">",$start_date." 00:00:00");
            
            if(!empty($end_date))$info = $info->where("remind_at","<",$end_date." 23:59:59");
            
            if(!empty($text))$info->where("content","like", "%$text%");
            
            $info = $info->get()->toArray();
            
            $info_array = Common::remind_show_date($info);
            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array_values($info_array));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "month_info", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
  
       /**
    * @Author:    zy
    * @DateTime:  2017-09-26
    * @Description: 返回当前用户当前月的提醒
    */
    public function month_info(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'date' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $start_date = Input::get("date") . "-01 00:00:00";
            $end_date =  date('Y-m-t 23:59:59', strtotime($start_date));
            $info = Remind::where("shop_id",$sc_user->shop_id)
                    ->where("remind_at",">",$start_date)
                    ->where("remind_at","<",$end_date)
                    ->get();
            
            $response = array('code' => 0, 'msg' => 'ok', 'result' =>$info );
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "month_info", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
   
       /**
    * @Author:    zy
    * @DateTime:  2017-09-26
    * @Description: 修改提醒
    */
    public function update_remind(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'remind_id' => 'required',
                
            )
        );
//                'color' => 'required',
//                'remind_by' => 'required',
//                'remind_at' => 'required',
//                'content' => 'required',
//                'user_type' => 'required',
//                'status' = 1;
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            $Remind = Remind::find(Input::get("remind_id"));
            $Remind->color = Input::get("color",$Remind->color);
            $Remind->remind_by = Input::get("remind_by",$Remind->remind_by);
            $Remind->remind_at = Input::get("remind_at",$Remind->remind_at).":00";
            $Remind->content = Input::get("content",$Remind->content);
            $Remind->user_type = Input::get("user_type",$Remind->user_type);
            $Remind->created_by = $sc_user->id;
            $Remind->status = Input::get("status",$Remind->status);
            $Remind->shop_id = $sc_user->shop_id;
            
            $result = $Remind->save() ? TRUE : false ;
            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "update_remind", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
       /**
    * @Author:    zy
    * @DateTime:  2017-09-26
    * @Description: 返回当前用户当前月的提醒
    */
    public function info(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'remind_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            $remind_id = Input::get("remind_id");
            $info = Remind::where("id",$remind_id)
                    ->where("shop_id",$sc_user->shop_id)
                    ->get();
            
            $response = array('code' => 0, 'msg' => 'ok', 'result' =>$info );
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "month_info", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
}
